<template>
  <div>
    <el-dialog title="统计"
               width="800px"
               :visible="true"
               :show-close="false">
      <el-button type="primary"
                 plain
                 @click="drawChart(1)">柱状图</el-button>
      <el-button type="primary"
                 plain
                 @click="drawChart(0)">折线图</el-button>
      <el-button type="primary"
                 plain
                 @click="drawChart(2)">饼状图</el-button>
      <div id="chart"
           style="width:100%; height:400px;"></div>
      <el-button type="primary"
                 plain
                 @click="handleClose">关闭</el-button>
    </el-dialog>
  </div>
</template>

<script>
import echarts from "echarts";
export default {
  props: {
    tableData: {
      type: Array
    },
    flag: Number
  },
  data() {
    return {
      datax: [],
      datay: [],
      items: []
    };
  },
  methods: {
    handleClose() {
      this.$emit("closedialog");
    },
    drawChart(flag) {
      this.chart = echarts.init(document.getElementById("chart"));
      if (flag == 0) {
        this.chart.setOption({
          title: {
            text: "工时数据"
          },
          tooltip: {
            trigger: "axis"
          },
          toolbox: {
            show: true,
            feature: {
              dataZoom: {
                yAxisIndex: "none"
              },
              dataView: { readOnly: false },
              restore: {},
              saveAsImage: {}
            }
          },
          xAxis: {
            type: "category",
            data: this.datax
          },
          yAxis: {
            type: "value",
            axisLabel: {
              formatter: "{value} "
            }
          },
          series: [
            {
              name: "工时数据",
              type: "line",
              data: this.datay
            }
          ]
        });
      } else if (flag == 1) {
        this.chart.setOption({
          title: {
            text: "工时数据"
          },
          tooltip: {
            trigger: "axis"
          },
          toolbox: {
            show: true,
            feature: {
              dataZoom: {
                yAxisIndex: "none"
              },
              dataView: { readOnly: false },
              restore: {},
              saveAsImage: {}
            }
          },
          xAxis: {
            type: "category",
            data: this.datax
          },
          yAxis: {
            type: "value",
            axisLabel: {
              formatter: "{value} "
            }
          },
          series: [
            {
              name: "工时数据",
              type: "bar",
              data: this.datay
            }
          ]
        });
      } else {
        this.chart.setOption({
          title: {
            text: "工时数据",
            x: "center"
          },
          tooltip: {
            trigger: "item",
            formatter: "{a} <br/>{b} : {c} ({d}%)"
          },
          legend: {
            orient: "vertical",
            left: "left",
            data: this.datax
          },
          series: [
            {
              name: "工时数据",
              type: "pie",
              radius: "55%",
              center: ["50%", "50%"],
              data: this.items,
              itemStyle: {
                emphasis: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: "rgba(0, 0, 0, 0.5)"
                }
              }
            }
          ]
        });
      }
      // this.chart.
    }
  },
  mounted: function() {
    if (this.flag == 0) {
      for (var i = 0; i < this.tableData.length - 1; i++) {
        this.datax.push(this.tableData[i].activity);
        this.datay.push(this.tableData[i].rowSum);
        this.items.push({
          value: this.tableData[i].rowSum,
          name: this.tableData[i].activity
        });
      }
    } else if (this.flag == 1) {
      for (var i = 0; i < this.tableData.length; i++) {
        var y = 0;
        for (var j = 0; j < this.tableData[i].effort.length; j++) {
          y += this.tableData[i].effort[j];
        }
        this.datax.push(this.tableData[i].activities);
        this.datay.push(y);
        this.items.push({
          value: y,
          name: this.tableData[i].activities
        });
      }
    } else if (this.flag == 2) {
      for (var i = 0; i < this.tableData.length; i++) {
        var y = 0;
        for (var j = 0; j < this.tableData[i].effort.length; j++) {
          y += this.tableData[i].effort[j];
        }
        this.datax.push(this.tableData[i].name);
        this.datay.push(y);
        this.items.push({
          value: y,
          name: this.tableData[i].name
        });
      }
    }
    setTimeout(() => {
      this.drawChart(2);
    }, 500);
  }
};
</script>

<style scoped>
</style>
